package com.runshu.newsmartark.mvp.presenter;

import android.os.AsyncTask;
import android.text.TextUtils;

import com.google.gson.Gson;
import com.runshu.newsmartark.dbutil.RefreshUtils;
import com.runshu.newsmartark.entity.ConfirmStockEntity;
import com.runshu.newsmartark.entity.NewTakeStockEntity;
import com.runshu.newsmartark.entity.ProductsBean;
import com.runshu.newsmartark.mvp.contract.ConfirmStockContract;
import com.runshu.newsmartark.mvp.contract.ProductsByCategoryContract;
import com.runshu.newsmartark.network.netbean.ResponseBean;
import com.runshu.newsmartark.network.netinterface.MyCallBack;
import com.runshu.newsmartark.util.RefUtil;

import java.util.ArrayList;
import java.util.List;

/**
 * Demo class
 *
 * @author yu
 * @date 2020/10/28
 * @decs：
 */

public class ConfirmStockPresenter implements ConfirmStockContract.Presenter {
    private ConfirmStockContract.View view;
    private ConfirmStockContract.Model model;

    public ConfirmStockPresenter(ConfirmStockContract.View view, ConfirmStockContract.Model model) {
        this.view = view;
        this.model = model;
    }


    @Override
    public void getConfirmStock(String macAddress, String userCode, List<String> refIds, String type) {
        OwnTask task = new OwnTask( view );
        task.execute( refIds );
//
//        model.getConfirmStock( macAddress, userCode, refIds, type, new MyCallBack() {
//            @Override
//            public void onSuccess(ResponseBean data) {
//                view.OnGetConfirmStockSuccess( data );
//            }
//
//            @Override
//            public void onError(ResponseBean data) {
//                view.onError( data.getMessage() );
//            }
//
//            @Override
//            public void onErrorForOthers(ResponseBean msg) {
//                view.onError( msg.getMessage() );
//            }
//        } );
    }

    static class OwnTask extends AsyncTask<List<String>, Integer, ConfirmStockEntity> {
        private ConfirmStockContract.View view;

        public OwnTask(ConfirmStockContract.View view) {
            this.view = view;
        }

        @Override
        protected ConfirmStockEntity doInBackground(List<String>... lists) {
            List<String> list = new ArrayList<>();
            for (String tmp : lists[0]) {
                if (TextUtils.isEmpty( tmp )) continue;
                String e = RefUtil.decryptRfid( tmp );
                if (TextUtils.isEmpty( e )) continue;
                list.add( e );
            }
            ConfirmStockEntity result = RefreshUtils.getInstance().getGoodsExchange( list.toArray( new String[list.size()] ) );
            return result;
        }

        @Override
        protected void onPostExecute(ConfirmStockEntity confirmStockEntity) {
            super.onPostExecute( confirmStockEntity );
            if (confirmStockEntity == null) return;
            ResponseBean responseBean = new ResponseBean();
            responseBean.setData( new Gson().toJson( confirmStockEntity ) );
            responseBean.setCode( "200" );
            responseBean.setMessage( "成功" );
            responseBean.setClass( ConfirmStockEntity.class );
            view.OnGetConfirmStockSuccess( responseBean );
        }
    }
}